package com.nuc.bigdata.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.exception.BusinessException;
import com.nuc.bigdata.model.ResultCode;
import com.nuc.bigdata.orm.dto.NewsCommentsDto;
import com.nuc.bigdata.orm.entity.PhotoCommentsLike;
import com.nuc.bigdata.orm.entity.PhotoNewsComments;
import com.nuc.bigdata.orm.feign.PhotoNewsCommentsFeign;
import com.nuc.bigdata.orm.mapper.PhotoCommentsLikeMapper;
import com.nuc.bigdata.orm.mapper.PhotoNewsCommentsMapper;
import com.nuc.bigdata.service.PhotoNewsCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/27
 */
@Service
public class PhotoNewsCommentsServiceImpl implements PhotoNewsCommentsService {
    private final PhotoCommentsLikeMapper commentsLikeMapper;
    private final PhotoNewsCommentsMapper commentsMapper;
    private final static Long LENGTH = 3L;

    @Autowired
    PhotoNewsCommentsServiceImpl(PhotoCommentsLikeMapper commentsLikeMapper,
                                 PhotoNewsCommentsMapper commentsMapper){
        this.commentsLikeMapper = commentsLikeMapper;
        this.commentsMapper = commentsMapper;
    }


    /**
     * 获取单个图片资讯的评论
     * @param pnId 图片资讯的唯一标识
     * @param pageSize 页码
     * @return 提供给front服务的评论feign类的ArrayList
     */
    @Override
    public ArrayList<PhotoNewsCommentsFeign> listById(String pnId, Long pageSize) {
        ArrayList<PhotoNewsCommentsFeign> commentsFeignArrayList = new ArrayList<>();
        Long offset = (pageSize-1)*LENGTH;
        ArrayList<PhotoNewsComments> commentsList = commentsMapper.listByPnId(pnId, offset, LENGTH);
        for (PhotoNewsComments comments:commentsList){
            PhotoNewsCommentsFeign commentsFeign = transformComments(comments);
            commentsFeignArrayList.add(commentsFeign);
        }
        return commentsFeignArrayList;
    }

    /**
     * 给图片资讯评论
     * @param pnId 图片资讯唯一标识
     * @param newsCommentsDto 评论内容类包含userId和评论内容content
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createComments(String pnId, NewsCommentsDto newsCommentsDto) {
        PhotoNewsComments comments = new PhotoNewsComments();
        String photoNewsCommentId = IdUtil.fastSimpleUUID();
        comments.setComment(newsCommentsDto.getContent());
        comments.setPnId(pnId);
        comments.setPhotoCommentId(photoNewsCommentId);
        comments.setCreatedBy(newsCommentsDto.getUserId());
        commentsMapper.insert(comments);
    }

    /**
     * 点赞评论
     * @param userId 用户唯一标识
     * @param photoCommentId 评论唯一标识
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toCommentLike(String userId, String photoCommentId) {
        PhotoCommentsLike commentsLike = new PhotoCommentsLike();
        commentsLike.setPhotoCommentId(photoCommentId);
        commentsLike.setCreatedBy(userId);
        QueryWrapper<PhotoCommentsLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("photo_comment_id",photoCommentId );
        queryWrapper.eq("created_by", userId);
        PhotoCommentsLike photoCommentsLike = commentsLikeMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(photoCommentsLike)){
            commentsLikeMapper.insert(commentsLike);
        }else {
            throw new BusinessException(ResultCode.COMMENT_LIKE_IS_EXIST,photoCommentsLike);
        }


    }

    /**
     * 转化图片评论实体为图片评论feign
     * @param comments 评论实体
     * @return 图片评论feign
     */
    private PhotoNewsCommentsFeign transformComments(PhotoNewsComments comments){
        PhotoNewsCommentsFeign commentsFeign = new PhotoNewsCommentsFeign();
        commentsFeign.setComment(comments.getComment());
        commentsFeign.setCommentLike(comments.getCommentLike());
        commentsFeign.setCreatedBy(comments.getCreatedBy());
        commentsFeign.setCreateTime(comments.getCreateTime());
        commentsFeign.setPhotoCommentId(comments.getPhotoCommentId());
        commentsFeign.setPnId(comments.getPnId());
        return commentsFeign;
    }
}
